#include "stdafx.h"
#include "FPSCamera.h"
#include "InfoBoard.h"

CFpsCamera::CFpsCamera()
:m_fMoveSpeed(0.00001f)
, m_bLMouseDown(false)
{

}

CFpsCamera::~CFpsCamera()
{

}

void CFpsCamera::ProcessUserInput(UINT aMessage, WPARAM aWParam, LPARAM aLParam)
{
	switch( aMessage )
	{
	case WM_LBUTTONDOWN:
		{
			m_bLMouseDown = true;
			POINT pt = OS::GetMousePos();
			m_vMousePos.x = pt.x/800.0f;
			m_vMousePos.y = pt.y/600.0f;
		}
		break;
	case WM_LBUTTONUP:
		m_bLMouseDown = false;
		break;
	default:
		break;
	}
	return ;
}

void CFpsCamera::Logic( DWORD aDwTime )
{
	if ( m_bLMouseDown )
	{
		POINT pt = OS::GetMousePos();
		D3DXVECTOR3 pt2;

		pt2.x = pt.x/800.0f;
		pt2.y = pt.y/600.0f;
		pt2.z = sqrt(pt2.x*pt2.x + pt2.y*pt2.y);
		
		m_vMousePos.z = sqrt(m_vMousePos.x*m_vMousePos.x + m_vMousePos.y*m_vMousePos.y);
		
		float angle = acos( D3DXVec3Dot(&m_vMousePos, &pt2) );
		Log("%f", angle);

		//g_pInfoBoard->PushFrameInfo("cursor pos: %d,%d", pt.x, pt.y);
		
		//g_pInfoBoard->PushFrameInfo("v1(%f,%f,%f) Len:%f", m_vMousePos.x, m_vMousePos.y, m_vMousePos.z, D3DXVec3Length(&m_vMousePos));
		//g_pInfoBoard->PushFrameInfo("v2(%f,%f,%f) Len:%f", pt2.x, pt2.y, pt2.z, D3DXVec3Length(&pt2));

		m_CameraData->Yaw(pt2.x - m_vMousePos.x);
		m_CameraData->Updata(); 
		m_CameraData->Pitch(pt2.y - m_vMousePos.y);

		m_vMousePos = pt2;
	}

	//m_CameraData->Pitch(0.01);

	D3DXMATRIX* pViewMatrix = m_CameraData->GetViewMatrix();

	D3DXMATRIX pMatScaling;
	D3DXMatrixScaling(&pMatScaling, 2.0f, 2.0f, 2.0f);
	D3DXVECTOR3 vTst(1.0f, 1.0f, 1.0f);
	D3DXVECTOR4 vOut;
	D3DXVec3Transform(&vOut, &vTst, &pMatScaling);
	
	D3DXMATRIX pInverse;
	D3DXMatrixInverse(&pInverse, NULL, pViewMatrix);

	//D3DXVec3Transform(&vOut, &vTst, &pInverse);

	vTst.x = 0.0f;
	vTst.y = 0.0f;
	vTst.z = 0.0f;
	bool bKeyDown = false;
	if ( OS::IsKeyDown(VK_LEFT) || OS::IsKeyDown(L'A') )
	{
		bKeyDown = true;
		vTst.x = -1.0f;
	}

	if ( OS::IsKeyDown(VK_RIGHT) || OS::IsKeyDown(L'D') )
	{
		bKeyDown = true;
		vTst.x = 1.0f;
	}

	if ( OS::IsKeyDown(VK_UP) || OS::IsKeyDown(L'W') )
	{
		bKeyDown = true;
		vTst.z = 1.0f;
	}

	if ( OS::IsKeyDown(VK_DOWN) || OS::IsKeyDown(L'S') )
	{
		bKeyDown = true;
		vTst.z = -1.0f;
	}

	D3DXVec3Transform(&vOut, &vTst, &pInverse);
	

	D3DXVECTOR3& eyePos = m_CameraData->GetEyePos();

	D3DXVECTOR3 LookDir(vOut.x, vOut.y, vOut.z);
	LookDir -= eyePos;
	LookDir *= 0.05f;

	if (bKeyDown)
	{
		m_CameraData->Move(LookDir.x, LookDir.y, LookDir.z);
	}
	
	m_CameraData->Updata();
}

void CFpsCamera::Render()
{

}
